package com.cskaoyan.market.service.wx;

import com.cskaoyan.market.bo.RecommandBo;
import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketOrderMapper;
import com.cskaoyan.market.db.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author: YaLiFeng
 * @description: TODO
 * @date: 2024/5/21 11:55
 */
@Service
public class WxTopicServiceImpl implements WxTopicService {
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;


    @Override
    public List<MarketTopic> list(Integer page, Integer limit) {
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        PageHelper.startPage(page, limit);
        List<MarketTopic> marketTopicList = marketTopicMapper.selectByExample(marketTopicExample);
        return marketTopicList;

    }

    @Override
    public Map<String, Object> detail(Integer id) {
        Map<String, Object> map = new HashMap<>();
        //主题表以及主题的查询
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        //查询goods
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        List<MarketGoods> marketGoods = new ArrayList<>();
        for (int i = 0; i < marketTopic.getGoods().length; i++) {
            MarketGoodsExample.Criteria criteriaGoods = marketGoodsExample.createCriteria();
            criteriaGoods.andIdEqualTo(marketTopic.getGoods()[i]);
            MarketGoods marketGood = marketGoodsMapper.selectOneByExample(marketGoodsExample);
            marketGoods.add(marketGood);

        }
        map.put("goods", marketGoods);
        map.put("topic", marketTopic);
        return map;


    }

    //查询该用户的订单详情，将其对应的商品id对应的topic推荐
    @Override
    public List<MarketTopic> related(Integer id, Integer limit, Integer page) {
        //查询用户对应的订单，找到用户购买过的商品id
        //通过商品id去查询有哪些主题
        ///将这些主题作为推荐的主题返回
        Session session = SecurityUtils.getSubject().getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");
        //查询到用户所购买的商品
        List<RecommandBo> recommandBoList = getRelateData(user.getId());
        //查询对应用户的商品的tpoic作为推荐
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        //查询主题表
        List<MarketTopic> marketTopicList = marketTopicMapper.selectByExample(marketTopicExample);
        List<MarketTopic> recommandTopic = new ArrayList<>();
        //查询主题表是否有对应的商品
        int[] goodsId = new int[recommandBoList.size()];
        for (int i = 0; i < recommandBoList.size(); i++) {
            goodsId[i] = recommandBoList.get(i).getGoodsId();
        }
        String goods = new String(Arrays.toString(goodsId));

            for (int j = 0; j <marketTopicList.size(); j++) {
                MarketTopic marketTopic=marketTopicList.get(j);
                Integer[] topicGoods=marketTopic.getGoods();
                for (int i = 0; i < topicGoods.length; i++) {
                    if(goods.contains(String.valueOf(topicGoods[i]))) {
                        if (!recommandTopic.contains(marketTopic)) {
                            recommandTopic.add(marketTopic);
                            continue;
                        }
                    }

                }

            }
        return recommandTopic;


    }





    //查询用户表和订单表，凑在一块
    public List<RecommandBo> getRelateData(Integer id) {
        List<RecommandBo> recommandBosList = new ArrayList<>();
        //查询到订单表
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria1 = marketOrderExample.createCriteria();
        criteria1.andUserIdEqualTo(id);
        //查询到对应用户的订单表
        List<MarketOrder> marketOrderList = marketOrderMapper.selectByExample(marketOrderExample);

        //取出需要的数据
        for (int i = 0; i < marketOrderList.size(); i++) {
            //查询到对应的订单表
            MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
            MarketOrderGoodsExample.Criteria criteria = marketOrderGoodsExample.createCriteria();
            criteria.andOrderIdEqualTo(marketOrderList.get(i).getId());
            MarketOrderGoods marketOrderGoods = marketOrderGoodsMapper.selectOneByExample(marketOrderGoodsExample);
            if(marketOrderGoods==null){
                continue;
            }
            RecommandBo recommandBo = new RecommandBo();
            recommandBo.setUserId(id);
            recommandBo.setOrderId(marketOrderGoods.getOrderId());
            recommandBo.setGoodsId(marketOrderGoods.getGoodsId());
            recommandBo.setGoodsName(marketOrderGoods.getGoodsName());
            recommandBosList.add(recommandBo);

        }
        return recommandBosList;

    }
}
